package com.yass.zaika;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
//import android.widget.Button;
import android.widget.Spinner;

public class MealTypeTime extends Activity{

	private Spinner meal_type;
	private Spinner meal_time;
	private Zaika application;
	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mealtypetime);
		setVariables();
		intializeSpinners();
		dialog = new ProgressDialog(this);
		application = (Zaika)getApplication();
	}

	public void setVariables(){
		meal_type = (Spinner)findViewById(R.id.spmeal_type);
		meal_time = (Spinner)findViewById(R.id.spmeal_time);
	}

	//Method to set drop down menus of the respective spinners 
	public void intializeSpinners(){

		ArrayAdapter<CharSequence> mealTypeAdapter = ArrayAdapter.createFromResource(this, R.array.meal_type, android.R.layout.simple_spinner_item);
		mealTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		meal_type.setAdapter(mealTypeAdapter);

		ArrayAdapter<CharSequence> mealTimeAdapter = ArrayAdapter.createFromResource(this, R.array.meal_time, android.R.layout.simple_spinner_item);
		mealTimeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		meal_time.setAdapter(mealTimeAdapter);

	}

	public void clickButton(View v){

		switch(v.getId()){
		case R.id.submit_button:{
			// clear the results every time
			application.getDisplay().result_set.clear();
			application.getInput().setMealType(meal_type.getSelectedItem().toString());
			
			int mealTime = Integer.parseInt(meal_time.getSelectedItem().toString().substring(0, 2));
			application.getInput().setMealTime(mealTime);
			
			new doProcessTask().execute("");
			
			break;
		}

		}

	}
	
	private class doProcessTask extends AsyncTask<String, Void, Boolean> {
		@Override
		protected void onPreExecute() {
	        dialog.setMessage("Searching for recipes...");
	        dialog.setIndeterminate(true);
	        dialog.show();
	    }

	    @Override
	    protected void onPostExecute(final Boolean success) {
	        if(dialog.isShowing()) {
	            dialog.dismiss();
	        }
	        
	        if(application.getDisplay().result_set.size() == 0) {
	        	Toast.makeText(getApplicationContext(), "No recipes found...",
						Toast.LENGTH_LONG).show();
	        }
	        else {
	        	Intent myIntent = new Intent(MealTypeTime.this, ResultsForm.class);
	        	MealTypeTime.this.startActivity(myIntent);
	        }
	    }
		
		@Override
		protected Boolean doInBackground(String... file) {
			// process the inputs
			boolean ret = application.getProcess().processInputs(application.getInput().getIngredients(), application.getInput().getMealTime(), application.getInput().getMealType(), application);
			return ret;
		}

	}

}
